<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript" src="/assets/global/plugins/jquery.min.js"></script>
     
</head>
<body> 

<script type="text/javascript">
/**
 * 传入选号 计算注数
 * @param  {[data] 配置数据}  {"need_row": 2, "need_number": 1, "multiple": 1, "input": 1, "notes": {} , "checkboxs":{} }
 * @param  {[number_repeat] 选号是否重复} 1
 * @param  {[select_num] 选号} ["23", "36", "44"] // 行号+球号 23 第三[下标从0开始]行第3个号
 * @param  {[rows] 每行选号数} [2,1,1]
 * @param  {[cbx_count] checkbox 勾选数 } 3
 * @param  {[number_format] 号码格式 } 1,2
 * @return {[int] 注数}
 */
function count_note(data, number_repeat, select_num, rows, cbx_count, number_format){
    console.log(data);
    console.log(number_repeat);
    console.log(select_num);
    console.log(rows);
    console.log(cbx_count);
    // $.ajax({
    //  url:"/cn/cgi/select_api",
    //  type:"POST",
    //  data:{
    //      data:function(){ return JSON.stringify(data); },
    //      number_repeat:function(){ return number_repeat; },
    //      select_num:function(){ return JSON.stringify(select_num); },
    //      rows:function(){ return JSON.stringify(rows); },
    //      cbx_count:function(){ return cbx_count; },
    //  },
    //  success:function(msg){

    //  }
    // });

    var layout_code = data.layout_code;
    var need_row = Number(data.need_row+"");
    var need_number = Number(data.need_number+""); 
    var multiple = Number(data.multiple+""); 
    var input = Number(data.input+""); 
    var notes = data.notes;
    var is_cb = data.checkboxs==undefined?0:1; //是否有checkbox
    var counts = 0;
    number_format = number_format==undefined?1:number_format;

    //手动输入
    if(input>0){
        arr = select_num;
        var num_count = 0;
        for(var i=0;i<arr.length;i++){
            var item = arr[i];
            if(isNaN(item)){
                continue;
            }
            if(item==""){
                continue;
            }
            if(number_format==1){
                if(item.length==need_row){
                    counts++;
                }
            }
            if(number_format==2){
                if(parseInt(item)==0){
                    continue;
                }
            }
            num_count++;
        }
        if(number_format==2){
            counts = parseInt(num_count/need_row);
        }
        if(is_cb>0){
            var comb = number_combinations(cbx_count, need_row);
            counts *= comb;
        }
        // window.webkit.messageHandlers.sbbbb.postMessage(counts);
        return counts;
    }
    

    //组选位置
    if(is_cb>0){
        row = number_combinations(cbx_count,need_row);
        var num = select_num.length;
        num = number_combinations(num,need_number);
        counts = num*row*multiple;
        // window.webkit.messageHandlers.sbbbb.postMessage(counts);
        return counts;
    }
    

    //和值
    var is_notes = 0;
    for(var key in notes){
        is_notes++;
    }
    if(is_notes>0){
        for(var a=0;a<select_num.length;a++){ 
            for(var key in notes){
                var val = select_num[a];
                if(Number(key)==Number(val)){
                    counts+=Number(notes[key]);
                }
            }
        }
        // window.webkit.messageHandlers.sbbbb.postMessage(counts);
        return counts;
    }


    combs = unique_combination(rows, need_row);
    //多行组合
    for(var a=0;a<combs.length;a++){
        var item = combs[a];
        var item_count =0;
        for(var b=0;b<item.length;b++){
            //行内组合
            var com = number_combinations(item[b], need_number)*multiple;
            if(item_count==0){
                item_count = com;
                continue;
            }
            item_count*=com;
        }
        counts+=item_count;
    }


    //选号无重复
    if(number_repeat==0){
        combs = unique_combination(select_num, need_row);
        for(var i=0;i<combs.length;i++){
            var item = combs[i];
            var all = new Array();
            for(var a=0;a<item.length;a++){
                if(all.indexOf(item[a][0])>=0){
                    continue;
                }
                all.push(item[a][0]);
            }
            if(all.length<need_row){
                continue;
            }
            var arr = new Array();
            for(var a=0;a<item.length;a++){
                if(arr.indexOf(item[a][1]+""+item[a][2])<0){
                    arr.push(item[a][1]+""+item[a][2]);
                }
                else{
                    counts--;
                    break;
                }
            }
        }
    }
    // window.webkit.messageHandlers.sbbbb.postMessage(counts);
    return counts;      
}



function factorial($n) {
    if ($n <= 1) {
        return 1;
    } else {
        return factorial($n - 1) * $n;
    }
}
//组合反回结果数不包括组合列表
function number_combinations($n, $k) {
    if ($n < $k) {
        window.webkit.messageHandlers.sbbbb.postMessage(0);
        return 0;
    } else {
        var counts = factorial($n)/(factorial($k)*factorial(($n - $k)));
        window.webkit.messageHandlers.sbbbb.postMessage(counts);
        return counts;
    }
}
//组合反回组合列表
function unique_combination($arr, $sel){

    var aa = getCombination($arr,$sel);
    for(var i=0;i<aa.length;i++){
        var arr = (aa[i]+"").split(",");
        aa[i] = arr; 
    }
    return aa;

    // $list = new Array();
    // $all = 0;
    // for ($i=0;$i< Math.pow(2, $arr.length);$i++){
    //  $a = 0;
    //     $b = new Array();
    //     for ($j=0;$j<$arr.length;$j++){
    //         if ($i>>$j&1){
    //             $a++;
    //             $b.push($arr[$j]); 
    //         }
    //     }
    //     if ($a==$sel){
    //         $list.push($b);
    //         $all++;
    //     }
    // }
    // return $list;
}
function getCombination(o, c) {
    var l = o.length;
    var r = new Array();
    var f = new Array();
    if (c > l) {
        return r
    }
    if (c == 1) {
        return o
    }
    if (l == c) {
        r[0] = o.join(",");
        return r
    }
    var a = "";
    var b = "";
    var s = "";
    for (var g = 0; g < c; g++) {
        a += "1";
        b += "1"
    }
    for (var e = 0; e < l - c; e++) {
        a += "0"
    }
    for (var d = 0; d < c; d++) {
        s += o[d] + ","
    }
    r[0] = s.substr(0, s.length - 1);
    var h = 1;
    s = "";
    while (a.substr(a.length - c, c) != b) {
        a = movestring(a);
        for (var d = 0; d < l; d++) {
            if (a.substr(d, 1) == "1") {
                s += o[d] + ","
            }
        }
        r[h] = s.substr(0, s.length - 1);
        s = "";
        h++
    }
    return r
}
function movestring(a) {
    var h = "";
    var k = "01";
    var b = "";
    var f = "";
    var j = "";
    var g = false;
    var c = false;
    for (var e = 0; e < a.length; e++) {
        if (g == false) {
            h += a.substr(e, 1)
        }
        if (g == false && a.substr(e, 1) == "1") {
            c = true
        } else {
            if (g == false && c == true && a.substr(e, 1) == "0") {
                g = true
            } else {
                if (g == true) {
                    b += a.substr(e, 1)
                }
            }
        }
    }
    h = h.substr(0, h.length - 2);
    for (var d = 0; d < h.length; d++) {
        if (h.substr(d, 1) == "1") {
            f += h.substr(d, 1)
        } else {
            if (h.substr(d, 1) == "0") {
                j += h.substr(d, 1)
            }
        }
    }
    h = f + j;
    return h + k + b
}
    function get_random(min,max,number_format,data){
        min = Number(min+"");
        max = Number(max+"");
        var w = max - min;
        var r = Math.round(Math.random()*w+min);
        // console.log(r+","+w+","+max+","+min);
        if(data!=undefined){
            return data[r];
        }
        if(number_format==2){
            if(r<10){
                r = "0"+r;
            }
        }
        return r;
    }

    /**
     * [generate_item 生成随机注 依赖函数 get_random ]
     * @param  {[type]} config        [配置数据] {rule_id:143,need_row:1,need_number:1,input:0,selects:[{},{},{},{},{}],notes:{},multiple:1,max_odds:{0:"9.8"},checkboxs:null}
     * @param  {[type]} number_repeat [从item接口获取]
     * @param  {[type]} number_format [从item接口获取]
     * @param  {[type]} number_range  [从item接口获取]
     * @return {[type]}               [description]
     */
    function generate_item(config,number_repeat,number_format,number_range){
        // console.log(JSON.stringify(config));
        //var config = eval("("+config+")");        
        number_repeat = Number(number_repeat+"");
        var rs = { rule_id:config.rule_id, pick_number: "", position:"" };
        var need_row = Number(config.need_row+"");
        var need_number = Number(config.need_number+"");
        var input = Number(config.input+"");
        var number_ranges = number_range.split("-");
        var selects = config.selects;
        var notes = config.notes;
        rs.amount_per_note = 2;
        rs.note_count = config.multiple;
        rs.odds = config.max_odds[0];
        rs.rakeback=0;

        //checkbox
        if(config.checkboxs!=undefined){
            var arr = ["万位","千位","百位","十位","个位"];
            var pn = "";
            var all = ",";
            for(var i=0;i<need_row;i++){
                var r = get_random(0,4,1);
                while(all.indexOf(","+r+",")>=0){
                    r = get_random(0,4,1);
                }
                all+=r+",";
                pn+=pn==""?"":",";
                pn+=arr[r];
            }
            rs.position = pn;
        }
        
        //手动
        if(input>0){
            var all = ",";
            for(var i=0;i<need_row;i++){
                var r = get_random(number_ranges[0],number_ranges[1],number_format);
                while(all.indexOf(","+r+",")>=0){
                    r = get_random(number_ranges[0],number_ranges[1],number_format);
                }
                all+=r+",";
                if(number_format==2){
                    rs.pick_number+=rs.pick_number==""?"":" ";
                }
                rs.pick_number+=r;
            }
            window.webkit.messageHandlers.sbbbb.postMessage([rs]);
            return [rs];
        }

        var min = number_ranges[0];
        var max = number_ranges[1];

        var data = undefined;
        
        if(selects.length>0){
            data = selects[0].content;
            max = data.length-1;
        }
        
        //多行
        var pn = "";
        var start = get_random(0,selects.length-need_row,1);
        var all = ",";
        for(var i=0;i<selects.length;i++){
            var num = "";
            if(i>=start && i<start+need_row){
                for(l=0;l<need_number;l++){
                    var r = get_random(0,max,number_format,data);
                    while(all.indexOf(","+r+",")>=0){
                        r = get_random(0,max,number_format,data);
                    }
                    all+=r+",";
                    num+=num==""?"":",";
                    num+=r;
                }
            }
            pn+=num;
            pn+="|";
        }

        pn = pn.substr(0,pn.length-1);
        rs.pick_number=pn;
        //New add
        // var send_config = '{need_row:"'+need_row+'",need_number:"'+need_number+'",multiple:"'+rs.note_count+'",input:"'+input+'",notes:("'+config.notes+'")}';
        // var p_number = rs.pick_number.split('|');
        // var p_str = '';
        // for(i=0;i<p_number;i++) {

        // }
        // var return_note_count = count_note(config,number_repeat,selects[0].content,);

        var contains = rs.pick_number.indexOf(",");
            var checkboxNum = 0;
            if(config.checkboxs!=undefined){
                if(contains == -1){
                    checkboxNum = rs.pick_number.length;
                } else {
                    var shuzuNum = rs.pick_number.split(",");
                    checkboxNum = shuzuNum.length;
                }
            }
            var shuzu2 = rs.pick_number.split("|");
            //console.log("fff" + shuzu2);
            var shuzu3 = new Array();
            var shuzu4 = new Array();
            var hang = 0;
            for (var val in shuzu2) {
                console.log("jinlai");
                if(input > 0){//手写
                    if(shuzu2[val] != ""){
                        shuzu4.push(shuzu2[val]);
                    }
                } else {
                    if(shuzu2[val] != ""){
                        var fen = shuzu2[val].split(",");
                        shuzu3.push(fen.length);
                        //console.log("ddd" + shuzu2[val]);
                        for (var i = 0; i < fen.length; i++) {
                            // shuzu4.push(hang + "" + (i + 1));
                            shuzu4.push(hang + "" + selects[hang].content.indexOf(fen[i]));
                        }
                    }
                    hang++;
                }
            }

            //console.log(config);
            //console.log(number_repeat);
            //console.log(shuzu4);
            //console.log(shuzu3);
            //console.log(checkboxNum);
            var all_contents = count_note(config, number_repeat, shuzu4, shuzu3, checkboxNum);
        //new add over
            rs.note_counts = all_contents;
            console.log(all_contents);
        //New add over
        window.webkit.messageHandlers.sbbbb.postMessage([rs]);
        return [rs];
    }

    /**
    * 六合彩
    */

//从一个给定的数组arr中,随机返回num个不重复项
function getArrayItems(arr, num) {
    var temp_array = new Array();
    for (var index in arr) {
        temp_array.push(arr[index]);
    }
    var return_array = new Array();
    for (var i = 0; i<num; i++) {
        if (temp_array.length>0) {
            var arrIndex = Math.floor(Math.random()*temp_array.length);
            return_array[i] = temp_array[arrIndex];
            temp_array.splice(arrIndex, 1);
        } else {
            break;
        }
    }
    return return_array;
}

//模拟php in_array函数
function inArray(needle, array) {
    var length = array.length;
    for(var i = 0; i < length; i++) {
        if(array[i] == needle) return true;
    }
    return false;
}


//连接字符串
function joinstr(odd) {
    var odd_str = '';
    var j=0;
    for(var i in odd) {
        if (j==0) {
            odd_str += odd[i];
        } else {
            odd_str += ',' + odd[i];
        }
        j++;
    } 
    return odd_str;
}


/**
* 随机一注 六合彩
* amount_per_note : 单注金额
* note_count : 注数
* zmgg_rule_id : layout_code=’ zhengmaguoguan’ 的rule_id（只有正码过关需要传rule_id，剩余传空值）
* rakeback : layout_code=’ zhengmaguoguan’ 的max_rakeback（只有正码过关需要传rakeback，剩余传0）
* node : 玩法json节点 （正码过关传入odds节点）
*/
function generate_item_lhcs(zmgg_rule_id, rakeback, note_count, node) {
    //window.webkit.messageHandlers.sbbbb.postMessage('a');
    //console.log(JSON.stringify(node))
    //console.log(zmgg_rule_id)
    //console.log(rakeback)
    //console.log(note_count)
    //var node = eval('(' + node + ')');

    //if(!rakeback){
     //   rakeback = 0;
    //}

    //if(!zmgg_rule_id){
      //  zmgg_rule_id = '';
    //}

    var rs = {rule_id:node['rule_id'], pick_number:"",position:"", amount_per_note:amount_per_note, note_count:note_count,rakeback:"" };
    var amount_per_note = 2;
    rs.amount_per_note = amount_per_note;
    rs.note_count = note_count;
    rs.is_follow = 0;
    rs.is_win_stop = 0;
    var need_number = parseInt(node['need_number']);

    //数组1-49
    var arr_49 = [];
    l = 0;
    for (var j=1;j<=49;j++) {   
        if(j<10){
            j = "0"+j;
        }
        arr_49[l] = j;
        l++;
    }

    //数组0-9
    var arr_10 = [0,1,2,3,4,5,6,7,8,9];

    //数组生肖、五行
    var arr_sx = ['鼠','牛','虎','兔','龙','蛇','马','羊','猴','鸡','狗','猪'];
    var arr_wx = ['金','木','水','火','土'];

    if (node instanceof Array && node.length >= 1) {
        //window.webkit.messageHandlers.sbbbb.postMessage('b');
        //正码过关
        if (zmgg_rule_id!='0') {
            var offset = get_random(2,6,1);
            var zmgg_odd = 1;
            for(var i=0,l=offset;i<l;i++){
                var num = get_random(0,12);
                var zmgg_odd_2 = Math.round(zmgg_odd*100)/100;
                zmgg_odd = zmgg_odd_2*node[i][num]['odds'];
                zmgg_odd = Math.round(zmgg_odd*100)/100;
                if (i==0) {
                    rs.pick_number+=i;
                    rs.position+=num;
                } else {
                    rs.pick_number+=','+i;
                    rs.position+=','+num;
                }
            }
            rs.rule_id = zmgg_rule_id;
            rs.odds = zmgg_odd;
            rs.rakeback = rakeback;
            //window.webkit.messageHandlers.sbbbb.postMessage([rs]);
            //console.log(rs);
            MyCount.getRandomString(JSON.stringify(rs));
            return [rs];
        }

        //生肖
        if (node[0]['layout_code'].indexOf("shengxiao")!=-1 || node[0]['layout_code'].indexOf("zhengxiao")!=-1 || node[0]['layout_code'].indexOf("yixiao")!=-1 || node[0]['layout_code'].indexOf("zongxiao")!=-1 || node[0]['layout_code'].indexOf("sebo")!=-1  || node[0]['layout_code'].indexOf("banbo")!=-1 || node[0]['layout_code'].indexOf("banbanbo")!=-1 || node[0]['layout_code'].indexOf("qisebo")!=-1 || node[0]['layout_code'].indexOf("touweishu")!=-1 || node[0]['layout_code'].indexOf("zhengteweishu")!=-1 || node[0]['layout_code'].indexOf("qimawuxing_qima")!=-1 || node[0]['layout_code'].indexOf("qimawuxing_wuxing")!=-1) {
            var result = getArrayItems(node,need_number);
            if (node[0]['layout_code'].indexOf("zongxiao")!=-1) {
                var end = 5;
            } else if (node[0]['layout_code'].indexOf("sebo")!=-1) {
                var end = 2;
            } else if (node[0]['layout_code'].indexOf("qisebo")!=-1) {
                var end = 3;
            } else if (node[0]['layout_code'].indexOf("touweishu")!=-1) {
                var end = 14;
            } else if (node[0]['layout_code'].indexOf("zhengteweishu")!=-1) {
                var end = 9;
            } else if (node[0]['layout_code'].indexOf("qimawuxing_qima")!=-1) {
                var end = 15;
            } else if (node[0]['layout_code'].indexOf("qimawuxing_wuxing")!=-1) {
                var end = 4;
            } else {
                var end = 11;  
            }
            var random = get_random(0,end,1);
            rs.rule_id = node[random]['rule_id'];
            rs.pick_number = node[random]['name'];
            rs.odds = node[random]['max_odds'][0];
            rs.rakeback = node[random]['max_rakeback'][0];
            // window.webkit.messageHandlers.sbbbb.postMessage([rs]);
            MyCount.getRandomString(JSON.stringify(rs));
            return [rs];
        }

        //两面 - 其他
        if (node[0]['layout_code']=='liangmian_tedan') {
            var random = get_random(0,11,1);
            rs.rule_id = node[random]['rule_id'];
    
            rs.pick_number = node[random]['name'];
            rs.odds = node[random]['max_odds'][0];
            rs.rakeback = node[random]['max_rakeback'][0];
            // window.webkit.messageHandlers.sbbbb.postMessage([rs]);
            MyCount.getRandomString(JSON.stringify(rs));
            return [rs];
        }
        //两面 - 正码1-6
        if (node[0]['layout_code'].indexOf("liangmian_zhengma")!=-1 || node[0]['layout_code'].indexOf("zhengma16_zhengma")!=-1) { 
            //window.webkit.messageHandlers.sbbbb.postMessage('c');
            var returns = getArrayItems(node,1);
            rs.rule_id = returns[0]['rule_id'];
            rs.pick_number = returns[0]['name'];
            rs.odds = returns[0]['max_odds'][0];
            rs.rakeback = returns[0]['max_rakeback'][0];
            // window.webkit.messageHandlers.sbbbb.postMessage([rs]);
            MyCount.getRandomString(JSON.stringify(rs));
            return [rs];
        }

    }

    //特码AB - 其他
    if (node instanceof Array && node.length >= 3) {
        if (node[2]['layout_code']=='temaab_tedan') {
            var random = get_random(2,15,1);
            rs.rule_id = node[random]['rule_id'];
            rs.pick_number = node[random]['name'];
            rs.odds = node[random]['max_odds'][0];
            rs.rakeback = node[random]['max_rakeback'][0];
            // window.webkit.messageHandlers.sbbbb.postMessage([rs]);
            MyCount.getRandomString(JSON.stringify(rs));
            return [rs];
        }
    }

    //49球
    if (node['layout_code']) {
        //连肖 连尾
        if (node['layout_code'].indexOf("lianxiao")!=-1 || node['layout_code'].indexOf("lianwei")!=-1) {
            if (node['layout_code'].indexOf("lianxiao")!=-1) {
                var result = getArrayItems(arr_sx,need_number);
            }
            if (node['layout_code'].indexOf("lianwei")!=-1) {
                var result = getArrayItems(arr_10,need_number);
            }
            
            var length = result.length;
            var odd = [];
            var lianwei = [];
            for (var i=0;i<length;i++) {
                odd[i] = node['max_odds'][result[i]];
                lianwei[i] = '尾'+result[i];
            }

            if (node['layout_code'].indexOf("lianxiao")!=-1) {
                rs.pick_number = result.join();
            }
            if (node['layout_code'].indexOf("lianwei")!=-1) {
                rs.pick_number = lianwei.join();
            }

            var new_odd = odd.sort();
            rs.odds = new_odd[0];
            rs.rule_id = node['rule_id'];
            rs.rakeback = node['max_rakeback'];
            // window.webkit.messageHandlers.sbbbb.postMessage([rs]);
            MyCount.getRandomString(JSON.stringify(rs));
            return [rs];
        }

        //连码三中二
        if (node['layout_code'].indexOf("lianma_sanzhonger")!=-1 || node['layout_code'].indexOf("lianma_erzhongte")!=-1) {
            var result = getArrayItems(arr_49,need_number);
            var length = result.length;
            rs.pick_number = result.join();
            rs.odds = node['max_odds'][0]+','+node['max_odds'][1];
            rs.rule_id = node['rule_id'];
            rs.rakeback = node['max_rakeback'];
            // window.webkit.messageHandlers.sbbbb.postMessage([rs]);
            MyCount.getRandomString(JSON.stringify(rs));
            return [rs];
        }

        //合肖
        //中 赔率计算公式：选中赔率总和/(选中个数)^2
        //不中 赔率计算公式：
        if (node['layout_code'].indexOf("hexiao")!=-1) {
            var odds = node['max_odds'][0];
            var random = node['layout_code']=='hexiao_zhong'?10:10;
            var num = get_random(1,random,1);
            var result = getArrayItems(arr_sx,num);
            var length = result.length;
            var long_odds = node['layout_code']=='hexiao_zhong'?odds/length:odds/(12-num);
            rs.odds = Math.round(long_odds*1000)/1000;
            rs.pick_number = result.join();
            // window.webkit.messageHandlers.sbbbb.postMessage([rs]);
            //console.log(rs);
            MyCount.getRandomString(JSON.stringify(rs));
            return [rs];
        }

        //自选不中 
        if (node['layout_code'].indexOf("zixuanbuzhong")!=-1 || node['layout_code'].indexOf("zhongyi")!=-1 || node['layout_code'].indexOf("lianma_siquanzhong")!=-1 || node['layout_code'].indexOf("lianma_sanquanzhong")!=-1 || node['layout_code'].indexOf("lianma_erquanzhong")!=-1 || node['layout_code'].indexOf("lianma_techuan")!=-1) {
            var result = getArrayItems(arr_49,need_number);
            rs.pick_number = result.join();
            rs.odds = node['max_odds'][0];
            rs.rule_id = node['rule_id'];
            rs.rakeback = node['max_rakeback'];
            // window.webkit.messageHandlers.sbbbb.postMessage([rs]);
            MyCount.getRandomString(JSON.stringify(rs));
            return [rs];
        }

        //特码ab、正码、正码特
        if (node['layout_code'].indexOf("tamaab_tema") || node['layout_code'].indexOf("zhengma")!=-1 || node['layout_code'].indexOf("zhengmate")!=-1) {
            var result = getArrayItems(arr_49,need_number);
            var length = result.length;
            var odd = [];
            for (var i=0;i<length;i++) {
                odd[i] = node['max_odds'][result[i]];
            }
            
            rs.pick_number = result.join();
            
            
            rs.odds = odd.join();
            rs.rule_id = node['rule_id'];
            rs.rakeback = node['max_rakeback'];
            // window.webkit.messageHandlers.sbbbb.postMessage([rs]);
            MyCount.getRandomString(JSON.stringify(rs));
            return [rs];
        }



    }

}
</script>

</body>
</html>
